package com.reremouse.servlet;

import com.reremouse.rerebbs.web.mb.ApplicationBean;
import com.reremouse.util.SpringHelper;
import org.apache.commons.io.IOUtils;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ShowAbstractImage1
        extends HttpServlet {
    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Image src = null;
        BufferedImage image = null;
        BufferedImage bimage = null;
        InputStream is = null;

        String id = request.getParameter("id");
        if (id != null) {
            String relativePath = SpringHelper.getSpringBean("relativeDir").toString() + id + ".jpg";

            String path = request.getServletContext().getRealPath(relativePath);
            File fff = new File(path);
            if (fff.exists()) {
                System.out.println(relativePath);

                RequestDispatcher rd = getServletContext().getRequestDispatcher("/" + relativePath);
                rd.forward(request, response);
            } else {
                response.setContentType("image/JPEG");
                OutputStream out = response.getOutputStream();

                StringBuilder dir = new StringBuilder();
                ApplicationBean ab = (ApplicationBean) request.getServletContext().getAttribute("applicationBean");
                dir.append(ab.getFilePath());
                dir.append("\\");
                dir.append(id);
                String nfn = dir.toString();
                try {
                    is = new BufferedInputStream(new FileInputStream(nfn));
                } catch (Exception e) {
                    String fp = request.getServletContext().getRealPath("resources/sys/default.jpg");

                    is = new FileInputStream(fp);
                    is = new BufferedInputStream(is);
                }
                try {
                    src = ImageIO.read(is);
                    int width = src.getWidth(null);
                    int height = src.getHeight(null);
                    image = new BufferedImage(width, height, 1);

                    Graphics2D g = image.createGraphics();
                    image.getGraphics().drawImage(src, 0, 0, null);
                    g.drawImage(src, 0, 0, null);
                    g.dispose();
                    g = null;

                    int w = 600;
                    String ww = request.getParameter("w");
                    if (ww != null) {
                        w = Integer.parseInt(ww);
                    }
                    bimage = getResizePicture(image, w);

                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ImageOutputStream imOut = ImageIO.createImageOutputStream(bos);
                    ImageIO.write(bimage, "jpg", imOut);

                    InputStream bis = new ByteArrayInputStream(bos.toByteArray());

                    bos.close();
                    IOUtils.copy(bis, out);
                    bis.close();
                    bos = null;
                    imOut = null;
                    bis = null;
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    is.close();
                    src = null;
                    image = null;
                    bimage = null;
                    out.flush();
                    response.setStatus(200);
                    response.flushBuffer();
                    out.close();
                }
            }
        }
    }

    public final BufferedImage getResizePicture(BufferedImage originalPic, int w) {
        int originalImageWidth = originalPic.getWidth();

        int originalImageHeight = originalPic.getHeight();

        double bo = w / originalImageWidth;


        int changedImageWidth = w;

        int changedImageHeight = (int) (originalImageHeight * bo);


        BufferedImage changedImage = new BufferedImage(changedImageWidth, changedImageHeight, originalPic.getType());


        changedImage.getGraphics().drawImage(originalPic, 0, 0, changedImageWidth, changedImageHeight, null);
        return changedImage;
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    public void init()
            throws ServletException {
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.servlet.ShowAbstractImage1

 * JD-Core Version:    0.7.0.1

 */